package com.hancher.common.androidutil;

import android.os.Looper;

import com.hancher.common.base.BaseApplication;
import com.kongzue.dialogx.DialogX;
import com.kongzue.dialogx.dialogs.PopTip;

/**
 * 作者: Hancher
 * 日期: 2020/9/6 12:09
 * 描述: Toast工具
 * https://github.com/Muddz/StyleableToast
 * Android11 后台服务不能使用自定义的Toast
 */
public class ToastUtil {

    private static void showToastInternal(String message, int icon) {
        DialogX.onlyOnePopTip = false;

        try {
            if (icon == -1) {
                PopTip.show(message);
            } else {
                PopTip.show(icon, message).setTintIcon(false);
            }
        } catch (Exception e) {
            Looper.prepare();
            if (icon == -1) {
                PopTip.show(message);
            } else {
                PopTip.show(icon, message).setTintIcon(false);
                ;
            }
            Looper.loop();
        }
    }

    public static void showErr(String message) {
        LogUtil.print(LogUtil.ERROR, "失败 Toast:" + message);
        showToastInternal(message, com.kongzue.dialogx.R.mipmap.ico_dialogx_error);
    }

    public static void showErr(Throwable message) {
        LogUtil.print(LogUtil.ERROR, "失败 Toast:" + message);
        showToastInternal(message.getMessage(), com.kongzue.dialogx.R.mipmap.ico_dialogx_error);
    }

    public static void showSuccess(String message) {
        LogUtil.print(LogUtil.ERROR, "成功 Toast:" + message);
        showToastInternal(message, com.kongzue.dialogx.R.mipmap.ico_dialogx_success);
    }

    public static void showAlert(String message) {
        LogUtil.print(LogUtil.ERROR, "警告 Toast:" + message);
        showToastInternal(message, com.kongzue.dialogx.R.mipmap.ico_dialogx_warning);
    }

    public static void show(int msg) {
        String message = BaseApplication.getInstance().getResources().getString(msg);
        LogUtil.print(LogUtil.DEBUG, "蓝色Toast:" + message);
        showToastInternal(message, logo);
    }

    public static void show(String message) {
        LogUtil.print(LogUtil.DEBUG, "蓝色Toast:" + message);
        showToastInternal(message, logo);
    }

    static int logo = -1;

    public static void init(int iconLogo) {
        logo = iconLogo;
    }
}
